package com.zoe.wan.android.example.activity.my_collects

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.blankj.utilcode.util.ToastUtils
import com.zoe.wan.android.example.R
import com.zoe.wan.android.example.activity.webView.WebActivity
import com.zoe.wan.android.example.common.ui.AppBar
import com.zoe.wan.android.example.repository.data.MyCollectItemListData

class MyCollectListActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyCollectListScreen()
        }
    }

    @OptIn(ExperimentalMaterial3Api::class)
    @Composable
    private fun MyCollectListScreen() {
        val viewModel: MyCollectListViewModel = viewModel()

        Column {
            AppBar(onBackClick = {
                finish()
            }, "我的收藏")
            CollectListView(viewModel)
        }

    }

    @Composable
    private fun CollectListView(viewModel: MyCollectListViewModel) {
        val collects = viewModel.collectList.observeAsState()
        LazyColumn {
            items(count = collects.value?.size ?: 0) { index ->
                val item = collects.value?.get(index)
                ListItem(viewModel, item) {
                    jumpToWeb(baseContext, item?.title, item?.link)
                }
            }
        }
    }

    @Composable
    private fun ListItem(viewModel: MyCollectListViewModel, item: MyCollectItemListData?, onClick: () -> Unit) {
        Column(modifier = Modifier
            .fillMaxWidth()
            .padding(dimensionResource(R.dimen.dp_10))
            .clickable {
                onClick.invoke()
            }) {
            Row(
                horizontalArrangement = Arrangement.SpaceBetween,
                modifier = Modifier.fillMaxWidth()
            ) {
                Text(text = "作者:${item?.author ?: "无"}")
                Text(text = "时间:${item?.niceDate}")
            }
            Spacer(
                modifier = Modifier.height(dimensionResource(R.dimen.dp_5))
            )
            //title
            Text(text = item?.title ?: "", fontSize = 16.sp)
            Spacer(
                modifier = Modifier.height(dimensionResource(R.dimen.dp_5))
            )
            Row(
                horizontalArrangement = Arrangement.SpaceBetween,
                modifier = Modifier.fillMaxWidth()
            ) {
                Text(text = "分类:${item?.chapterName ?: "无"}")
                //分享事件
                var icon = painterResource(id = R.drawable.img_collect)
                Image(
                    painter = icon,
                    contentDescription = "",
                    modifier = Modifier.size(
                        width = dimensionResource(R.dimen.dp_28),
                        height = dimensionResource(R.dimen.dp_28)
                    ).clickable {
                        viewModel.cancelCollect("${item?.originId}") {
                            ToastUtils.showShort("取消收藏成功")
                        }
                    }
                )
            }
            Spacer(
                modifier = Modifier.height(dimensionResource(R.dimen.dp_5))
            )
            Spacer(
                modifier = Modifier.background(Color.Gray)
                    .height(dimensionResource(R.dimen.dp_0_5))
                    .fillMaxWidth()
            )
        }
    }

    private fun jumpToWeb(context: Context, title: String?, url: String?) {
        val intent = Intent(context, WebActivity::class.java)
        intent.putExtra(WebActivity.INTENT_WEB_TITLE_KEY, title)
        intent.putExtra(WebActivity.INTENT_WEB_URL_KEY, url)
        startActivity(intent)
    }

}